I must admit that creating a nice type safe collection for a new feature in one of our products took me a little time. There are some very good resources online on how to setup your own type safe collection so I thought I’d contribute a little to that resource.
The key to all this working nicely is to set up a good base class that has a thin implementation of the IList
interface with one key addition…
1 |
protected virtual void OnValidate(object value){ if (!(value is ListObject)) { throw new ArgumentException("ListObjects only in this List."); }} |
This simple function in your base class is then used by pretty much all of the IList
implementation functions that take an ‘object’ as the argument. Of course we want to make sure that this ‘object’ is one of the correct type – we do that by calling OnValidate()
like…
1 |
public bool Contains(object value){ OnValidate(value); return m_InnerList.Contains(value);} |
So if you don’t pass an object of the correct type to one of the IList
implementations then we throw an ArgumentException
as one would expect. Now the cunning part is that we create this as part of a base class in our application and whenever we need a type safe collection we simply inherit from it and override
the OnValidate()
method.
1 |
protected override void OnValidate(object value){ base.OnValidate(value); if (!(value is FunkyObject)) { throw new ArgumentException("FunkyObjects only in this List."); }} |
Then if we’re being really really nice to our API users we can then go on and provide IList
-like functions that take the appropriate type as an argument so they know what they’re meant to be doing.
1 |
public bool Contains(InheritedObject funkyNewObject){ return base.Contains(funkyNewObject);} |
And there we have it, a simple way of producing a number of classes of type safe collections in .NET 1.1 C#. Of course this is what generics are for now although .NET 2 is still a little green.
Load comments